package cz.ctu.guiproject.client.formvalidation;

import java.util.List;

import org.apache.commons.lang3.StringUtils;

import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

public class Form<T extends ViewGroup> {

	private final ViewGroup viewGroup;

	public Form(T viewGroup) {
		this.viewGroup = viewGroup;
	}

	/**
	 * Returns true, if all fields within this form are valid
	 * 
	 * @return
	 */
	public EditText isValid() {
		List<View> possibleTextViews;
		for (int i = 0; i < viewGroup.getChildCount(); ++i) {

			View view = viewGroup.getChildAt(i);
			possibleTextViews = view.getFocusables(0);

			for (View possibleEditText : possibleTextViews) {
				if (possibleEditText instanceof EditText) {
					final EditText editText = (EditText) possibleEditText;
					if (!StringUtils.isBlank(editText.getError())) {
						return editText;
					}
				}
			}
		}
		return null;
	}
}
